<html>
<head>
  <title>abrance eth</title>
  <basefont face="Consolas" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="YXBJ Windows/602263 (zh-CN, DDL); Windows/10.0.0 (Win64); EDAMVersion=V2;"/>
  <style>
    body, td {
      font-family: Consolas;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="3384"/>
<h1>以太坊数据上链</h1>
<div>
<table bgcolor="#D4DDE5" border="0">
<tr><td><b>创建时间：</b></td><td><i>2020/10/9 9:43</i></td></tr>
<tr><td><b>更新时间：</b></td><td><i>2020/10/27 14:20</i></td></tr>
<tr><td><b>作者：</b></td><td><i>DaguguJ</i></td></tr>
</table>
</div>
<br/>

<div>
  <span><div><div><br/></div><table style="border-collapse: collapse; min-width: 100%;"><colgroup><col style="width: 220px;"></col><col style="width: 1028px;"></col></colgroup><tbody><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>why</div></li><ul><li><div>部署数据上链的业务，熟悉区块链（以太坊）的一般流程，针对新手快速入门区块链（以太坊）</div></li><li><div>本人做的所有教程都有两个目的 备忘、希望有人指正</div></li></ul><li><div>how</div></li><ul><li><div>搭建 流行的以太坊 私链服务器，实现数据上链，并将一系列流程 记录</div></li></ul><li><div>what</div></li><ul><li><div>初次 实现数据上链 笔记 </div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>查阅的资料</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div><span style="font-size: unset; color: unset; font-family: unset;">链社区</span></div></li><ul><li><div><a href="https://learnblockchain.cn/">https://learnblockchain.cn/</a></div></li></ul><li><div>solidity 教程</div></li><ul><li><div><a href="https://www.tryblockchain.org/Solidity-AccessorFunction-%E8%AE%BF%E9%97%AE%E5%87%BD%E6%95%B0.html">https://www.tryblockchain.org/Solidity-AccessorFunction-%E8%AE%BF%E9%97%AE%E5%87%BD%E6%95%B0.html</a></div></li><li><div><a href="https://dev.fo/zh-cn/guide/smart-contract-database.html">https://dev.fo/zh-cn/guide/smart-contract-database.html</a></div></li><li><div><a href="https://learnblockchain.cn/docs/solidity/layout-of-source-files.html#import">https://learnblockchain.cn/docs/solidity/layout-of-source-files.html#import</a></div></li></ul><li><div>eth 控制台命令</div></li><ul><li><div><a href="https://blog.51cto.com/12880687/2083802">https://blog.51cto.com/12880687/2083802</a></div></li><li><div><a href="https://www.jianshu.com/p/9fa31e4cdf4d">https://www.jianshu.com/p/9fa31e4cdf4d</a></div></li></ul><li><div>保存数据</div></li><ul><li><div><a href="https://www.cnblogs.com/tinyxiong/p/8084477.html">https://www.cnblogs.com/tinyxiong/p/8084477.html</a></div></li></ul><li><div>智能合约 编译部署</div></li><ul><li><div><a href="https://blog.csdn.net/fpcc/article/details/82746442">https://blog.csdn.net/fpcc/article/details/82746442</a></div></li></ul><li><div>python调接口</div></li><ul><li><div><a href="http://blog.hubwiz.com/2018/08/21/ethereum-python-json-rpc/">http://blog.hubwiz.com/2018/08/21/ethereum-python-json-rpc/</a></div></li><li><div><a href="https://web3py.readthedocs.io/en/latest/web3.main.html">https://web3py.readthedocs.io/en/latest/web3.main.html</a></div></li><li><div><a href="http://cw.hubwiz.com/card/c/ethereum-json-rpc-api/1/3/26/">http://cw.hubwiz.com/card/c/ethereum-json-rpc-api/1/3/26/</a></div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><br/></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>不足：</div></li><ul><li><div>此文难以阅读，描述和实例文件 跨度大，需要C-f 查找关键字</div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>源码链接</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div><a href="https://github.com/abrance/MedicalLeagueChain">https://github.com/abrance/MedicalLeagueChain</a></div></li></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>涉及语言</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>python</div></li><li><div>solidity</div></li></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>数据上链基本流程</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ol><li><div>部署环境</div></li><li><div>部署geth 服务</div></li><li><div>建立连接，启动矿工</div></li><li><div>编写 solidity 智能合约，并编译，得到abi和bytecode</div></li><li><div>部署合约</div></li><li><div>调用合约</div></li></ol></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>环境</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>一台香港服务器 debian系统</div></li><li><div><br/></div></li><li><div>nodejs安装</div></li><ul><li><div>npm 包</div></li></ul><li><div>git安装</div></li><li><div>python3 （3.7）</div></li><li><div>geth 安装</div></li><li><div>go 安装</div></li></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>genesis.json</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><span style="font-size: unset; color: unset; font-family: unset;">存储基本配置信息和额外配置信息，在写脚本 时可以直接导入文件内容（实例下面）</span></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>geth 启动服务参数</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>geth 连接服务</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>geth attach <a href="http://localhost:8545/">http://localhost:8545</a>    # 一般是这个端口 rpc连接</div></li><li><div>geth attach ipc:/data/gethdata/geth.ipc    # ipc 连接</div></li><li><div>...    # websocket 连接 没试过</div></li></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>geth 命令行操作</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px 0px 0px 40px; border: 1px solid transparent; font-size: medium; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><li style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; border: 1px solid transparent;"><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">eth.accounts</span></div></li><li style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; border: 1px solid transparent;"><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">查看所有账户</span></div></li><li style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; border: 1px solid transparent;"><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">eth.getBalance() 查看某一账户财产</span></div></li><li><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">eth.coinbase</span></div></li><li><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><br/></div></li><li><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">miner.start()</span></div></li><li><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">miner.stop()</span></div></li><li><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">...</span></div></li><li style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px; border: 1px solid transparent;"><div style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; margin: 0px; padding: 0px;"><span style="box-sizing: border-box; -webkit-tap-highlight-color: transparent; font-size: medium; color: rgb(0, 0, 0); font-family: Consolas; font-variant-caps: normal; font-variant-ligatures: normal;">exit    退出</span></div></li></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>solc 编译sol文件（不智能）</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>安装</div></li><ul><li><div>npm install -g solc</div></li></ul><li><div>编译</div></li><ul><li><div>solcjs storage.sol --bin --abi --optimize -o ./    # 编译storage.sol 文件，文件输出到同级目录出现 abi 和 bin文件 bin使用时需要加'0x'</div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>truffle 进行sol文件的智能编译</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>参考</div></li><ul><li><div><a href="https://truffle.tryblockchain.org/Truffle-CommandRefrence-%E5%91%BD%E4%BB%A4%E6%8C%87%E5%8D%97.html">https://truffle.tryblockchain.org/Truffle-CommandRefrence-%E5%91%BD%E4%BB%A4%E6%8C%87%E5%8D%97.html</a></div></li></ul><li><div>安装truffle</div></li><ul><li><div>npm install -g truffle</div></li></ul><li><div>基本使用</div></li><ul><li><div>mkdir contracts</div></li><li><div>cd contracts</div></li><li><div>truffle init</div></li><li><div>.... 自动建立项目 目录树</div></li><ul><li><div>build contracts migrations test truffle-config.js</div></li><li><div>这里只用到 contracts build两个目录</div></li></ul><li><div>写好一个 sol 文件，放入contracts目录</div></li><li><div>truffle compile</div></li><ul><li><div>设置版本在 <span style="font-size: 11.9px; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgba(27, 31, 35, 0.05); color: rgb(36, 41, 46); font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, monospace; font-variant-caps: normal; font-variant-ligatures: normal;">truffle-config.js 中设置</span></div></li><li><div><span style="font-size: 9pt;"><a href="https://github.com/trufflesuite/truffle/issues/1827" style="font-family: SFMono-Regular; font-size: 9pt;">https://github.com/trufflesuite/truffle/issues/1827</a></span></div></li></ul><li><div>这时 build/contracts 出现 同名.json 文件</div></li><li><div>完成，json文件备用，写脚本时、部署合约时使用</div></li></ul><li><div>第n次 进行编译</div></li><ul><li><div>truffle compile 默认只编译修改过的文件，要想编译新增的文件，需要加参数</div></li><li><div>truffle compile --compile-all</div></li></ul><li><div>部署</div></li><ul><li><div>第一种</div></li><ul><li><div>取build/contracts/*.json中 abi值和bytecode值进行合约的部署</div></li></ul><li><div>第二种</div></li><ul><li><div>truffle 自动部署</div></li></ul><li><div>第三种</div></li><ul><li><div>脚本部署</div></li></ul></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>metamask 使用</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>功能</div></li><ul><li><div>连接私链账户</div></li><li><div>连接remix部署智能合约</div></li><li><div>账户内容</div></li><ul><li><div>交易内容</div></li><li><div>修改交易细节</div></li><li><div>导出私钥</div></li><ul><li><div>三点-账户详情-导出私钥</div></li></ul><li><div>合约信息</div></li></ul></ul><li><div>读《精通以太坊》 第二章：以太坊基本概念-MetaMask入门 跟书走一遍流程可以学会 几乎所有内容（除了导出私钥）</div></li></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>remix 使用</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>连接私链 并 部署合约</div></li><ul><li><div>deploy &amp; run transaction-environment-injected web3 弹窗输入连接地址</div></li><li><div>选择有钱的账户</div></li><li><div>写sol文件</div></li><li><div>deploy</div></li><li><div>可以看到右下 控制台出现</div></li><ul><li><div>creation of Hello pending...    # 说明正在部署</div></li><li><div>[block:918 txIndex:0]</div></li><li><div>from: 0xCcD...965Ecto: Hello.(constructor)value: 1 weidata: 0x608...00000logs: 0hash: 0xf09...66a0b    # 部署成功</div></li></ul><li><div>服务器日志中也可以看到</div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>python web3.py 来写脚本调用geth</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>安装 web3.py</div></li><ul><li><div>pip3 install web3 报错 lru_dict cytoolz 有问题</div></li><li><div><br/></div></li><li><div>git clone https://github.com/ethereum/web3.py.git</div></li><li><div>apt-get install python3-dev    # 不进行这一步可能导致 报错</div></li><li><div>python3.7 setup.py install     # 避免连接错了python3版本</div></li></ul><li><div>web3 使用</div></li><ul><li><div>与pypi 里不同 ？</div></li><ul><li><div>使用web3.utils 报错，无utils，试了一下可以直接使用utils里内容</div></li></ul></ul><li><div>实例</div></li><ul><li><div>部署已编译合约 deploy.py </div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div><br/></div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><div><br/></div></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>tip</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>geth 编译问题，这在国外服务器应该不存在</div></li><ul><li><div>如果在类Unix 系统发生连接超时类问题，依次执行</div></li><ul><li><div>go env -w GOPROXY=https://goproxy.cn,direct</div></li><li><div>go env -w GOPRIVATE=.gitlab.com,.gitee.com</div></li><li><div>go env -w GOSUMDB=off</div></li><li><div>go env -w GOSUMDB=&quot;sum.golang.google.cn&quot;</div></li></ul><li><div>再继续即可</div></li></ul><li><div>交易</div></li><ul><li><div>miner</div></li><ul><li><div>metamask 进行交易时，一直pending; 我以为是gasprice问题，加高没用，后来执行 miner.start() 一瞬间完成；不知原因</div></li></ul><li><div>gas limit问题</div></li><ul><li><div>在交易时 报错exceeds block gas limit， 需要调高price ，或调低 gas 参数； 不确定 21000*gasPrice左右，不要太离谱</div></li></ul></ul><li><div>函数调用问题</div></li><ul><li><div>transact(</div></li><li><div>    {'from': user, 'gas': 900000, 'gasPrice': 10}</div></li><li><div>)</div></li><li><div>call()</div></li><li><div><a href="https://web3py.readthedocs.io/en/latest/contracts.html">https://web3py.readthedocs.io/en/latest/contracts.html</a> 好好参考文档，看看是不是需要调用这个阻塞函数 <span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">w3</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(102, 102, 102); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">.</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">eth</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(102, 102, 102); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">.</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">waitForTransactionReceipt</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">(</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">tx_hash</span><span style="box-sizing: border-box; font-size: 12px; white-space: pre; overflow: auto; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; color: rgb(64, 64, 64); font-family: SFMono-Regular, Menlo, Monaco, Consolas, &quot;Liberation Mono&quot;, &quot;Courier New&quot;, Courier, monospace; font-variant-caps: normal; font-variant-ligatures: normal; line-height: 1.4;">)</span></div></li></ul></ul></td></tr><tr><td style="width: 220px; padding: 8px; border: 1px solid;"><div>总结</div></td><td style="width: 1028px; padding: 8px; border: 1px solid;"><ul><li><div>很多方式可以 交易，数据上链，各种信息查询，但是做完觉得，脚本方式最简单</div></li></ul></td></tr></tbody></table><div><br/></div></div><div><br/></div></span>
</div></body>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/1.11.3/jquery.js"></script><script> $(function () {console.log($("a").attr("target", "_blank"));})</script>
</html> 
